package com.wsb.wetchat.config;

import org.slf4j.MDC;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import java.util.UUID;

/**
 * @author wang shaobo
 * @date 2022/6/30  10:20
 */
@WebFilter(urlPatterns = "/*")
@Order(1)
public class TraceIdFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        String traceId = servletRequest.getParameter("traceId");
        if(traceId == null || "".equals(traceId)){
            traceId = UUID.randomUUID().toString();
        }

        MDC.put("traceId",traceId);

        filterChain.doFilter(servletRequest,servletResponse);

    }
}
